package cn.iocoder.yudao.module.system.controller.admin.studentdashboard;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.controller.admin.studentdashboard.vo.*;
import cn.iocoder.yudao.module.system.service.studentdashboard.StudentDashboardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 学生看板 Controller
 *
 * @author 芋道源码
 */
@Tag(name = "管理后台 - 学生看板")
@RestController
@RequestMapping("/system/student-dashboard")
@Validated
@Slf4j
public class StudentDashboardController {

    @Resource
    private StudentDashboardService studentDashboardService;

    @GetMapping("/get")
    @Operation(summary = "获取学生看板数据")
    @Parameter(name = "studentId", description = "学生ID", required = true)
    public CommonResult<StudentDashboardRespVO> getStudentDashboard(@Valid StudentDashboardReqVO reqVO) {
        StudentDashboardRespVO dashboard = studentDashboardService.getStudentDashboard(reqVO);
        return success(dashboard);
    }

    @GetMapping("/ranking")
    @Operation(summary = "获取学生排名信息")
    public CommonResult<StudentRankingRespVO> getStudentRanking(@Valid StudentRankingReqVO reqVO) {
        StudentRankingRespVO ranking = studentDashboardService.getStudentRanking(reqVO);
        return success(ranking);
    }

    @GetMapping("/comparison")
    @Operation(summary = "获取学生考试对比数据")
    public CommonResult<StudentExamComparisonRespVO> getStudentExamComparison(@Valid StudentExamComparisonReqVO reqVO) {
        StudentExamComparisonRespVO comparison = studentDashboardService.getStudentExamComparison(reqVO);
        return success(comparison);
    }

    @GetMapping("/class-rank")
    @Operation(summary = "获取学生班级排名")
    @Parameter(name = "studentId", description = "学生ID", required = true)
    @Parameter(name = "examId", description = "考试ID", required = true)
    public CommonResult<Integer> getStudentClassRank(@RequestParam("studentId") Long studentId,
                                                     @RequestParam("examId") Long examId) {
        Integer rank = studentDashboardService.getStudentClassRank(studentId, examId);
        return success(rank);
    }

    @GetMapping("/grade-rank")
    @Operation(summary = "获取学生年级排名")
    @Parameter(name = "studentId", description = "学生ID", required = true)
    @Parameter(name = "examId", description = "考试ID", required = true)
    public CommonResult<Integer> getStudentGradeRank(@RequestParam("studentId") Long studentId,
                                                     @RequestParam("examId") Long examId) {
        Integer rank = studentDashboardService.getStudentGradeRank(studentId, examId);
        return success(rank);
    }

    @GetMapping("/subject-class-rank")
    @Operation(summary = "获取学生科目班级排名")
    @Parameter(name = "studentId", description = "学生ID", required = true)
    @Parameter(name = "examId", description = "考试ID", required = true)
    @Parameter(name = "subjectName", description = "科目名称", required = true)
    public CommonResult<Integer> getStudentSubjectClassRank(@RequestParam("studentId") Long studentId,
                                                            @RequestParam("examId") Long examId,
                                                            @RequestParam("subjectName") String subjectName) {
        Integer rank = studentDashboardService.getStudentSubjectClassRank(studentId, examId, subjectName);
        return success(rank);
    }

    @GetMapping("/subject-grade-rank")
    @Operation(summary = "获取学生科目年级排名")
    @Parameter(name = "studentId", description = "学生ID", required = true)
    @Parameter(name = "examId", description = "考试ID", required = true)
    @Parameter(name = "subjectName", description = "科目名称", required = true)
    public CommonResult<Integer> getStudentSubjectGradeRank(@RequestParam("studentId") Long studentId,
                                                            @RequestParam("examId") Long examId,
                                                            @RequestParam("subjectName") String subjectName) {
        Integer rank = studentDashboardService.getStudentSubjectGradeRank(studentId, examId, subjectName);
        return success(rank);
    }

    @GetMapping("/equivalent-score-rank")
    @Operation(summary = "获取学生的等效位次和等效分信息")
    @Parameter(name = "studentId", description = "学生ID", required = true)
    @Parameter(name = "examId", description = "考试ID", required = true)
    @PreAuthorize("@ss.hasPermission('system:student-dashboard:query')")
    public CommonResult<StudentDashboardRespVO.EquivalentScoreRankInfo> getStudentEquivalentScoreRank(
            @RequestParam("studentId") Long studentId,
            @RequestParam("examId") Long examId) {
        StudentDashboardRespVO.EquivalentScoreRankInfo result = 
                studentDashboardService.getStudentEquivalentScoreRankByExam(studentId, examId);
        return success(result);
    }

    @PostMapping("/calculate-equivalent-score-rank")
    @Operation(summary = "计算学生的等效位次和等效分")
    @Parameter(name = "studentId", description = "学生ID", required = true)
    @Parameter(name = "examId", description = "考试ID", required = true)
    @PreAuthorize("@ss.hasPermission('system:student-dashboard:calculate')")
    public CommonResult<StudentDashboardRespVO.EquivalentScoreRankInfo> calculateStudentEquivalentScoreRank(
            @RequestParam("studentId") Long studentId,
            @RequestParam("examId") Long examId) {
        StudentDashboardRespVO.EquivalentScoreRankInfo result = 
                studentDashboardService.calculateStudentEquivalentScoreRankByExam(studentId, examId);
        return success(result);
    }

    @GetMapping("/equivalent-rank-trend")
    @Operation(summary = "获取学生的等效位次趋势分析")
    @Parameter(name = "studentId", description = "学生ID", required = true)
    @Parameter(name = "examId", description = "考试ID", required = true)
    @PreAuthorize("@ss.hasPermission('system:student-dashboard:query')")
    public CommonResult<StudentDashboardRespVO.EquivalentRankTrendInfo> getEquivalentRankTrend(
            @RequestParam("studentId") Long studentId,
            @RequestParam("examId") Long examId) {
        StudentDashboardRespVO.EquivalentRankTrendInfo result = 
                studentDashboardService.getEquivalentRankTrendByExam(studentId, examId);
        return success(result);
    }

}
